﻿using UnityEngine;
using System.Collections;
namespace GameUnit
{
    public class Area
    {
        public enum AreaType
        {
            Rect = 2,
            Circle = 1,
        }
        public AreaType Type;
        public float Radius;
        public float FaceDirX;
        public float FaceDirY;
        public float Deg;

        public float RectWidth;
        public float RectHeight;
        public void Init(Hashtable config)
        {
            this.Type = (AreaType)SGLisp.Helper.GetMapValue<int>(config, ":type", 0);
            this.Radius = SGLisp.Helper.GetMapValue<float>(config, ":radius", 0f);
            this.FaceDirX = SGLisp.Helper.GetMapValue<float>(config, ":FaceDirX", 0f);
            this.FaceDirY = SGLisp.Helper.GetMapValue<float>(config, ":FaceDirY", 0f);
            this.Deg = SGLisp.Helper.GetMapValue<float>(config, ":Deg", 0f);

            this.RectWidth = SGLisp.Helper.GetMapValue<float>(config, ":RectWidth", 0f);
            this.RectHeight = SGLisp.Helper.GetMapValue<float>(config, ":RectHeight", 0f);
        }
        public bool IsInSide(float x, float y, int angle = 0)
        {
            switch (this.Type)
            {
                case AreaType.Circle:
                    return this.IsInSide_Circle(x, y,angle);
                case AreaType.Rect:
                    return this.IsInSide_Rect(x, y, angle);
            }
            return false;
        }

        bool IsInSide_Rect(float posX, float posY, int angle = 0)
        {
            if (posY > this.RectHeight * 0.5 && posY < -this.RectHeight * 0.5 && posX > this.RectWidth * 0.5 && posX < -this.RectWidth * 0.5)
            {
                return true;
            }
            return false;
        }
        bool IsInSide_Circle(float posX, float posY, int angle = 0)
        {
            Vector2 PosPoint = new Vector2(posX, posY);
            if (PosPoint.magnitude > this.Radius)
            {
                return false;
            }
            PosPoint.Normalize();
            Vector2 FaceDir = new Vector2(this.FaceDirX, this.FaceDirY);
            if (angle == -1)
            {
                FaceDir = new Vector2(-1,0);
            }
            FaceDir.Normalize();
            float posDeg =  (float)System.Math.Atan2((double)PosPoint.y, (double)PosPoint.x) * (180 / (float)System.Math.PI);
            float faceDeg = (float)System.Math.Atan2((double)FaceDir.y, (double)FaceDir.x) * (180 / (float)System.Math.PI);
            if (  faceDeg + this.Deg * 0.5f > posDeg && faceDeg - this.Deg * 0.5f < posDeg)
            {
                return true;
            }
            return false;
        }
    }
}
